package org.example;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionFactoryUtils {
    private static SqlSessionFactory sqlSessionFactory;

    static {

        try {
            String resource = "resources/mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            //Resources类：从类路径中加载资源。
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static SqlSessionFactory getSqlSessionFactory(){return sqlSessionFactory;}

    public static SqlSession  openSession(){
        return  sqlSessionFactory.openSession();
    }
}
